package com.zoe.onelink.logging.trace.feign.processor;

import com.zoe.onelink.core.util.http.HttpClient;
import com.zoe.onelink.logging.trace.http.util.TracedOnelinkHttpClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-10-11
 */
@RequiredArgsConstructor
@Slf4j
public class OnelinkTracedHttpClientBeanPostProcessor implements BeanPostProcessor {

    private final BeanFactory beanFactory;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof HttpClient && !(bean instanceof TracedOnelinkHttpClient)) {
            log.info("[OneLinkFrm] 使用[TracedOnelinkHttpClient]包装第三方请求客户端[HttpClient]");
            return new TracedOnelinkHttpClient((HttpClient) bean, beanFactory);
        }
        return bean;
    }
}
